﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Spinning.Spinning.Level;
using Microsoft.Xna.Framework;
using Microsoft.Devices.Sensors;
using Spinning.Spinning.Unitity;
using ShakeGestures;
using Spinning.Spinning.EntityModifiers;

namespace Spinning.Spinning.Entity.Scenes
{
    class PracticeWindScene : PracticeScene
    {
        AnimationSprite spriteSpinning, spriteArrow;
        Sprite spriteArrow2;
        CircleGesture cg;
        public override void OnLoad()
        {
            base.OnLoad();

            Player.Alpha = 0;

            cg = new CircleGesture();
            cg.Position = new Vector2(240, 550);
            cg.BigRadius = 260;
            cg.SmailRadius = 0;
            cg.ZIndex = Constants.debugText;
            //cg.OnLoad();
            TouchManager.RegisteTouchArea(cg);
            this.Attach(cg);
            cg.OnRunCircleEvent += GetPoint;
            cg.OnAreaChangeEvent += OnAreaChange;

            spriteSpinning = new AnimationSprite();
            spriteSpinning.textureName = "wind/big_spin";
            spriteSpinning.Position = new Vector2(240-157,550-131);
            pos_X = spriteSpinning.Position.X;
            spriteSpinning.ZIndex = Constants.player;
            spriteSpinning.OnLoad();
            spriteSpinning.SetAnimation(new Point(1, 2),new int[] {1});
            spriteSpinning.Alpha = 1;
            this.Attach(spriteSpinning);

            spriteArrow = new AnimationSprite();
            spriteArrow.textureName = "wind/arrow";
            spriteArrow.Position = new Vector2(240-206, 390);
            spriteArrow.ZIndex = Constants.player;
            spriteArrow.OnLoad();
            spriteArrow.SetAnimation(new Point(2, 2), new int[] { 0 });
            spriteArrow.Alpha = 1;
            this.Attach(spriteArrow);

            spriteArrow2 = new Sprite();
            spriteArrow2.textureName = "wind/arrow_orange";
            spriteArrow2.Position = new Vector2(240 - 170, 240);
            spriteArrow2.ZIndex = Constants.player;
            spriteArrow2.OnLoad();
            spriteArrow2.Alpha = 0;
            this.Attach(spriteArrow2);

            timeCountBar = new TimeCountBar(new Vector2(15, 20), 10, "wind");
            timeCountBar.OnGetThreePointEvent += OnGetThreePoint;
            timeCountBar.OnTimesUpEvent += DoFail;
            this.Attach(timeCountBar);

            ShakeGesturesHelper.Instance.ShakeGesture += Instance_ShakeGesture;
            ShakeGesturesHelper.Instance.MinimumRequiredMovesForShake = 2;
            ShakeGesturesHelper.Instance.Active = false;
        }

        bool flag = false;
        float pos_X;
        public void Instance_ShakeGesture(object sender, ShakeGestureEventArgs e)
        {
            spriteArrow2.Alpha = 0;
            spriteSpinning.SetChangeFrameAnimation(new int[] {0,1});
            flag = true;
            timeCountBar.IsStop = true;
            ShakeGesturesHelper.Instance.Active = false;
        }

        int _gameTime,count,_time;
        protected override void UpdateSelf(GameTime gameTime)
        {
            base.UpdateSelf(gameTime);
            int delta = gameTime.ElapsedGameTime.Milliseconds;
            if (flag)
            {
                _gameTime += delta;
                _time += delta;
                if (_gameTime / 1000 >= 1)
                {
                    _gameTime = _gameTime % 1000;
                    
                    count++;
                    if (count == 4) Game1.stageManager.GoToScene("successWind");
                }
                spriteSpinning.Position = new Vector2(pos_X + (float)Math.Sin(_time)*10, spriteSpinning.Position.Y);
            }

            if (isPause) cg.IsStop = true;
            if (!cg.isPress) spriteArrow.SetChangeFrameAnimation(new int[] { 0 });
            //for (int i = 0; i < cg.angleList.Count; i++) //angleList.count = 3// 
            //{
            //    if(cg.angleList[i])spriteArrow.SetChangeFrameAnimation(new int[] {i+1});
            //}
        }

        void OnAreaChange()
        {
            spriteArrow.SetChangeFrameAnimation(new int[] { adjustInt(cg.CurrentArea) });
        }

        int adjustInt(int a) 
        {
            switch (a)
            {
                case 0:
                    return 1;
                case 1:
                    return 3;
                case 2:
                    return 2;
            }
            return 0;
        }

        public void GetPoint() 
        {
            timeCountBar.GetPoint();
        }

        private void OnGetThreePoint()
        {
            spriteArrow.Alpha = 0;
            spriteArrow2.Alpha = 1;
            ShakeGesturesHelper.Instance.Active = true;
            timeCountBar.IsStop = true;
            //Game1.stageManager.GoToScene("successWind");
        }

        public override Scene Clone()
        {
            Scene scene = new PracticeWindScene();
            scene.Size = new Vector2(Constants.Window_Width, Constants.Window_Height);
            scene.OnLoad();
            scene.SortChildren();
            return scene;
        }
    }
}
